package com.atguigu.cloud.sevice.Impl;/**
 * @Auter zzh
 * @Date 2024/11/13
 */


import com.atguigu.cloud.entities.Order;
import com.atguigu.cloud.sevice.OrderService;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.atguigu.cloud.tool.load.page.QueryObject.BaseQueryObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import com.atguigu.cloud.mapper.OrderMapper;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper.*;



import java.util.Collections;
import java.util.List;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.sevice.Impl
 * @className: orderServiceImpl
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 17:59
 * @version: 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    OrderMapper orderMapper;
    //根据orderid查询
    @Override
    public Order getById(Integer orderid) {
        QueryWrapper<Order> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("orderid",orderid);
        Order order = orderMapper.selectOne(queryWrapper);
        return order;
    }
    //根据userid查询
    @Override
    public List<Order> getByUserid(Integer userid) {
        QueryWrapper<Order> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userid",userid);
        List<Order> list_order = orderMapper.selectList(queryWrapper);
        return list_order;
    }
    @Override
    public List<Order> getAll() {

        QueryWrapper<Order> queryWrapper= new QueryWrapper<>();
        List<Order> list_order = orderMapper.selectList(queryWrapper);
        return list_order;

    }

    @Override
    public int add(Order order) {
        orderMapper.insert(order);
        return order.getOrderid();
    }


    //不知道为什么,只有这里的delete使用mbp的用不了
    @Override
    public int delete(Integer id) {
//        int result_id = orderMapper.deleteByPrimaryKey(id);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",id);
        int result_id =orderMapper.delete(queryWrapper);
        return result_id;
    }

    @Override
    public int update(Order order) {
        int id = order.getOrderid();
        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",id);
        orderMapper.update(order,updateWrapper);
        return id;
    }



    @Override
    public PaginationResult<Order> paginationQueryByUserid(Integer userid, Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }

        int pageSize = 3;
        // 创建分页对象
        Page<Order> page = new Page<>(currentPage, pageSize);

        // 创建查询条件构造器
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        long totalCount = orderMapper.selectCount(queryWrapper);
        queryWrapper.eq("userid", userid); // 假设我们根据id来查询用户
        List<Order> list_page_order=orderMapper.selectPage(page, queryWrapper).getRecords();

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Order>(currentPage,pageSize,totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Order>(currentPage,pageSize,totalCount,list_page_order );

    }

    @Override
    public PaginationResult<Order> myPaginationQuery(Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        //分页对象,包含分页大小和分页号数
        BaseQueryObject qo =new BaseQueryObject(currentPage);

        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();

        // 获取查询的记录数
        long totalCount = orderMapper.selectCount(queryWrapper);
        // 获得查询的目标结果集,(这里就用基础的mybatis实现了)
        List<Order> list_order = orderMapper.PaginationQuery(qo.getStart(),qo.getPageSize());

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Order>(qo.getCurrentPage(),qo.getPageSize(),totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Order>(qo.getCurrentPage(),qo.getPageSize(),totalCount,list_order );
    }
}
